﻿package tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class VoteDemo {
	public static void main(String[] args) {
		Menu m = Factory.getMenu();
		m.showMenu();
	}
}

class Menu {
	Member mb[] = new Member[4];

	public void showMenu() {
		mb[0] = Factory.getMember(1, "A", 0);
		mb[1] = Factory.getMember(2, "B", 0);
		mb[2] = Factory.getMember(3, "C", 0);
		mb[3] = Factory.getMember(4, "D", 0);
		System.out.println("参与投票的人员为：");
		for (Member m : mb) {
			System.out.print(m.getId() + "-" + m.getName() + "\t");
		}
		System.out.println();
		InputData idata = new InputData();
		while (true) {
			System.out.println("请输入选号(1-4),输入0结束:");
			int i = idata.getInt();
			switch (i) {
			case 1:
				mb[0].setVoteCount(mb[0].getVoteCount() + 1);
				break;
			case 2:
				mb[1].setVoteCount(mb[1].getVoteCount() + 1);
				break;
			case 3:
				mb[2].setVoteCount(mb[2].getVoteCount() + 1);
				break;
			case 4:
				mb[3].setVoteCount(mb[3].getVoteCount() + 1);
				break;
			case 0:
				showResults();
				System.out.println("程序已退出");
				System.exit(1);
			default:
				System.out.println("输出错误！");

			}
		}
	}

	private void showResults() {
		Arrays.sort(mb);
		System.out.println("得票情况如下:");
		for (Member m : mb) {
			System.out.println(m.getId() + "-" + m.getName() + "票数:" + m.getVoteCount());
		}
		System.out.println("本次投票结束,获胜者为:" + mb[0].getId() + "号竞选者:" + mb[0].getName());
	}
}

class Factory {
	public static Member getMember(int id, String name, int voteCount) {
		Member m = new Member(id, name, voteCount);
		return m;
	}

	public static Menu getMenu() {
		Menu m = new Menu();
		return m;
	}
}

class Member implements Comparable<Member> {

	int id = 0;
	String name = null;
	int voteCount = 0;

	public Member() {
	}

	public Member(int id, String name, int voteCount) {
		this.id = id;
		this.name = name;
		this.voteCount = voteCount;
	}

	@Override
	public int compareTo(Member o) {
		if (this.voteCount > o.getVoteCount()) {
			return -1;
		}
		if (this.voteCount < o.getVoteCount()) {
			return 1;
		}
		return 0;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getVoteCount() {
		return voteCount;
	}

	public void setVoteCount(int voteCount) {
		this.voteCount = voteCount;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}

class InputData {
	BufferedReader br = null;

	public InputData() {
		br = new BufferedReader(new InputStreamReader(System.in));
	}

	public int getInt() {
		int tmp = 0;
		boolean f = true;
		try {
			while (f) {
				String str = br.readLine();
				if (str.matches("\\d")) {
					tmp = Integer.parseInt(str);
					f = false;
				} else {
					System.out.println("输入错误！" + "\n" + "请输入选号(1-4),输入0结束:");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tmp;
	}
}
